//
//  PhoneBindViewModel.swift
//  KoiAccounting
//
//  Created by 纪洪文 on 13.04.25.
//

import Foundation
class PhoneBindViewModel : KoiAccountingBaseViewModel {
    @Published var isLoading = false
    @Published var bindState = ""
    @Published var captchaState: String = ""
 
    func sendCaptcha(phone: String) {
        isLoading = true
        KoiAccountingNetApi.standard.sendCaptcha(phone: phone) { result in
            switch result {
            case .success:
                self.captchaState = "success"
                self.isLoading = false
            case .failure(let error):
                self.captchaState = "fail"
                self.isLoading = false
                logger.error("sendCaptcha fail \(error)")
            }
        }
    }
    
    func bindPhone(phone: String, captcha: String) {
        KoiAccountingNetApi.standard.phoneBind(phone: phone, captcha: captcha) { result in
            switch result {
            case .success:
                KoiAccountingUserManager.standard.updateUser(phone: phone)
                self.isLoading = false
                self.bindState = "success"
            case .failure(let failure):
                logger.error("bindPhone fail \(failure)")
                self.bindState = failure.netErrorMsg()
            }
        }
    }
}
